package cn.itsource.pet.controller;


import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.PageBean;
import cn.itsource.basic.utils.SystemContext;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.service.IShopService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.query.PetQuery;
import cn.itsource.pet.service.IPetService;
import cn.itsource.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IShopService shopService;

    @Autowired
    private IPetService petService;


    /**
     * 处理寻主信息
     *
     * @param pet
     * @param request
     * @return
     */
    @PostMapping
    public AjaxResult handleMessage(@RequestBody Pet pet, HttpServletRequest request) {
        try {
            //获取登录信息对象
            LoginInfo loginInfo = SystemContext.getLoginInfo(request);
            //获取店铺对象
            Shop shop = shopService.loadByLogininfoid(loginInfo.getId());
            //获取店铺
            pet.setShop(shop);
            //保存宠物
            petService.handleMessage(pet,loginInfo);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }


    /**
     * resetful风格： 以请求资源对应多个方法， 以请求方式定位具体的某个方法
     * 常见的请求方式：
     * GET: 查询单个数据
     * POST： 新增/修改
     * PATCH: 批量数据（查询数据，删除批量数据）
     * DELETE:删除单个数据
     * PUT: 新增/修改
     * <p>
     * 分页查询
     *
     * @param query
     * @return
     */
    @PatchMapping
    public PageBean<Pet> queryPage(@RequestBody PetQuery query, HttpServletRequest request) {
        LoginInfo loginInfo = SystemContext.getLoginInfo(request);
        //获取店铺对象
        Shop shop = shopService.loadByLogininfoid(loginInfo.getId());
        query.setShopId(shop.getId());
        return petService.queryPage(query);
    }


    //培训阶段 ：宠物删除: 应该删除    宠物    宠物详情      寻主信息              真实开发：很少会物理删除数据，一般删除都是逻辑删除(给表加一个字段：009正常  003 删除)


    /**
     * 添加或者修改宠物
     * @param pet  宠物对象  如果id为空就是新增，如果id不为空就是修改
     * @return
     */
    @PutMapping
    public AjaxResult save(@RequestBody Pet pet,HttpServletRequest request){
        try {
            LoginInfo loginInfo = SystemContext.getLoginInfo(request);
            //获取店铺对象
            Shop shop = shopService.loadByLogininfoid(loginInfo.getId());
            //设置店铺
            pet.setShop(shop);
            petService.save(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    //----------------------------以下是门户网站对应的接口----------------------------------------

    @PatchMapping("/queryHomePage")
    public PageBean<Pet> queryHomePage(@RequestBody PetQuery petQuery){
        return petService.queryPage(petQuery);
    }

    @GetMapping("{id}")
    public Pet get(@PathVariable("id") Long id){
        return petService.loadById(id);
    }
}
